﻿/*----------------------------------------------------------------
// Copyright (C) 2011 苏富特健康科技
// 版权所有。 
//
// 文件名：CheckBoxListExtension.cs
// 文件功能描述：HtmlHelper的扩展
//
// 
// 创建标识：2012-07-09 闵德康
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Text;

namespace WaiGaoQiaoWeb.Content.Extensions
{
    public class InputListItem
    {
        public MvcHtmlString Button { get; set; }
        public MvcHtmlString Text { get; set; }
    }

    public static class CheckBoxListExtension
    {
        #region CheckBoxList

        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(
            this HtmlHelper<TModel> html,
            Expression<Func<TModel, TProperty>> expression,
            IEnumerable<SelectListItem> selectList,
            Func<dynamic, object> format,
            object htmlAttributes)
        {
            return CheckBoxListFor(
                html,
                expression,
                selectList,
                format,
                HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }


        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(
            this HtmlHelper<TModel> html,
            Expression<Func<TModel, TProperty>> expression,
            IEnumerable<SelectListItem> selectList,
            Func<dynamic, object> format = null,
            IDictionary<string, object> htmlAttributes = null)
        {
            return CheckBoxList(html,
                GetName(expression),
                selectList,
                format,
                htmlAttributes);
        }


        public static MvcHtmlString CheckBoxList(
          this HtmlHelper html,
          string name,
          IEnumerable<SelectListItem> selectList,
          Func<dynamic, object> format,
          object htmlAttributes)
        {
            return CheckBoxList(html,
                name,
                selectList,
                format,
                HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }


        public static MvcHtmlString CheckBoxList(
           this HtmlHelper html,
           string name,
           IEnumerable<SelectListItem> selectList,
           Func<dynamic, object> format = null,
           IDictionary<string, object> htmlAttributes = null)
        {
            return InputListInternal(html, name, selectList, true, format, htmlAttributes);
        }
        #endregion


        #region RadioButtonList
        public static MvcHtmlString RadioButtonList(
         this HtmlHelper html,
         string name,
         IEnumerable<SelectListItem> selectList,
         Func<dynamic, object> format,
         object htmlAttributes)
        {
            return RadioButtonList(html, name, selectList, format, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
        public static MvcHtmlString RadioButtonList(
         this HtmlHelper html,
         string name,
         IEnumerable<SelectListItem> selectList,
         Func<dynamic, object> format = null,
         IDictionary<string, object> htmlAttributes = null)
        {
            return InputListInternal(html, name, selectList, false, format, htmlAttributes);
        }
        public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(
          this HtmlHelper<TModel> html,
          Expression<Func<TModel, TProperty>> expression,
          IEnumerable<SelectListItem> selectList,
          Func<dynamic, object> format,
          object htmlAttributes)
        {
            return RadioButtonList(html, GetName(expression), selectList, format, htmlAttributes);
        }
        public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(
            this HtmlHelper<TModel> html,
            Expression<Func<TModel, TProperty>> expression,
            IEnumerable<SelectListItem> selectList,
            Func<dynamic, object> format = null,
            IDictionary<string, object> htmlAttributes = null)
        {
            return RadioButtonList(html, GetName(expression), selectList, format, htmlAttributes);
        }
        #endregion


        /*-------------------------------------
         * Core Function
         --------------------------------------*/
        private static MvcHtmlString InputListInternal(
            this HtmlHelper html,
            string name,
            IEnumerable<SelectListItem> selectList,
            bool allowMultiple,
            Func<dynamic, object> format,
            IDictionary<string, object> htmlAttributes
           )
        {
            string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (string.IsNullOrEmpty(fullHtmlFieldName))
            {
                throw new ArgumentException("filed can't be null or empty !", "name");
            }
            if (format == null)
                format = i => i.Button + i.Text + "<br/>";
            StringBuilder strBuilder = new StringBuilder();
            int z = 0;
            foreach (var item in selectList)
            {
                
                TagBuilder tagBuilder = new TagBuilder("input");
                if (allowMultiple)//checkbox
                {
                    tagBuilder.MergeAttribute("type", "checkbox", true);
                }
                else//radio
                {
                    tagBuilder.MergeAttribute("type", "radio", true);
                }
                tagBuilder.MergeAttribute("value", item.Value, true);
                if (item.Selected)
                    tagBuilder.MergeAttribute("checked", "checked", true);
                tagBuilder.MergeAttributes<string, object>(htmlAttributes);
                tagBuilder.MergeAttribute("name", fullHtmlFieldName, true);
                int idNumber = z++;//设置checkboxlist 每项ID
                tagBuilder.MergeAttribute("id", fullHtmlFieldName + idNumber.ToString());
                var btnHtmlString = new MvcHtmlString(tagBuilder.ToString());
                
                //the text for checkbox
                tagBuilder = new TagBuilder("label");
                tagBuilder.MergeAttribute("for", fullHtmlFieldName + idNumber.ToString());
                tagBuilder.SetInnerText(item.Text);

                var lblHtmlString = new MvcHtmlString(tagBuilder.ToString());
                var inputItem = new InputListItem { Button = btnHtmlString, 
                    Text = lblHtmlString };
                var s = format(inputItem).ToString();
                strBuilder.Append(s);
            }
            return new MvcHtmlString(strBuilder.ToString());
        }


        private static string GetName(LambdaExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            return ExpressionHelper.GetExpressionText(expression);
        }
    }
}